package com.wing.warehouse.service;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.wing.auth.service.AuthService;
import com.wing.common.utils.JsonResult;
import com.wing.redis.utils.redis.RedisHelperImpl;
import com.wing.set.entity.ChanjetSet;
import com.wing.set.service.ChanjetSetService;
import com.wing.util.ConstantsUtil;
import com.wing.util.HttpsUtils;
import com.wing.warehouse.entity.request.WarehouseCreateRequest;
import com.wing.warehouse.entity.request.WarehouseQueryRequest;
import com.wing.warehouse.entity.response.WarehouseQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 仓库管理
 */
@Service
@Slf4j
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    RedisHelperImpl redisHelper;

    @Resource
    AuthService authService;

    @Resource
    ChanjetSetService chanjetSetService;

    @Override
    public JsonResult warehouseCreate(WarehouseCreateRequest warehouseCreateRequest) {
        ChanjetSet chanjetSet = chanjetSetService.getById(1L);

        Map<String, String> headers = new HashMap<>();
        headers.put("appKey", chanjetSet.getAppKey());
        headers.put("appSecret", chanjetSet.getAppSecret());
        if(redisHelper.getValue("chanjetToken") == null){
            authService.getToken();
        }
        headers.put("openToken", redisHelper.getValue("chanjetToken").toString());

        Map<String, Object> param = new HashMap<>();
        param.put("dto", warehouseCreateRequest);

        String result = HttpsUtils.httpsPostByJson(ConstantsUtil.WAREHOUSE_CREATE, headers, JSON.toJSONString(param));
        //根据返回结果判断成功还是失败
        //1、创建成功  result:"3"     其中3代表新创建的仓库ID
        //2、创建失败 result:{ "RestException" : {"code":"AA10018","message":"仓库类型不能为空！","data":{"Code":"AA10018","StatusCode":400,"islogerror":"1"}} }

        return JsonResult.success();
    }

    @Override
    public List<WarehouseQueryResponse> warehouseQuery() {
        ChanjetSet chanjetSet = chanjetSetService.getById(1L);

        Map<String, String> headers = new HashMap<>();
        headers.put("appKey", chanjetSet.getAppKey());
        headers.put("appSecret", chanjetSet.getAppSecret());
        if(redisHelper.getValue("chanjetToken") == null){
            authService.getToken();
        }
        headers.put("openToken", redisHelper.getValue("chanjetToken").toString());

        WarehouseQueryRequest request = new WarehouseQueryRequest();
        request.setSelectFields("ID,Code,Name,WarehouseType,Address,Memo,HasPosition");
        Map<String, Object> param = new HashMap<>();
        param.put("param", request);

        String result = HttpsUtils.httpsPostByJson(ConstantsUtil.WAREHOUSE_QUERY, headers, JSON.toJSONString(param));
        Gson gson = new Gson();
        List<WarehouseQueryResponse> list = Arrays.asList(gson.fromJson(result, WarehouseQueryResponse[].class));
        return list;
    }
}
